﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Entity;
using LotteryVote.Logic.Web;
using LotteryVote.Logic;
using LotteryVote.Utility;
using LotteryVote.Logic.Application;

namespace LotteryVote
{
    public partial class BetSheetList : MemberPage
    {
        BetFacade betFacade = new BetFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            Execute(() =>
            {
                lv_pager.InitPager();
                if (!IsPostBack)
                    BindSheet();
            });
        }
        void BindSheet()
        {
            int totalCount;
            rpt_betsheet.DataSource = betFacade.GetTodayBetSheets(CurrentUser.UserId, LotterySystem.Current.PageItemCount, lv_pager.CurrentPager, out totalCount);
            rpt_betsheet.DataBind();
            lv_pager.BindPager(totalCount, LotterySystem.Current.PageItemCount);
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                hd_seach.Value = "1";
                BindSheet();
            });
        }

        protected void rpt_betsheet_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "cancel")
            {
                Execute(() =>
                {
                    int sheetId = Convert.ToInt32(e.CommandArgument);
                    betFacade.CancelBetSheet(sheetId, CurrentUser);
                    BindSheet();
                });
            }
        }
        decimal turnover, commission, netAmount;
        protected void rpt_betsheet_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                BetSheet data = e.Item.DataItem as BetSheet;
                if (data != null)
                {
                    turnover += data.Turnover;
                    commission += data.Commission;
                    netAmount += data.NetAmount;
                }
            }
            else if (e.Item.ItemType == ListItemType.Footer)
            {
                Literal ltlturnover = e.Item.FindControl("ltl_turnover") as Literal;
                Literal ltlcommisson = e.Item.FindControl("ltl_comm") as Literal;
                Literal ltlnetAmount = e.Item.FindControl("ltl_netAmount") as Literal;

                if (ltlturnover != null && ltlcommisson != null && ltlnetAmount != null)
                {
                    ltlturnover.Text = turnover.ToString();
                    ltlcommisson.Text = commission.ToString();
                    ltlnetAmount.Text = netAmount.ToString();
                }

            }
        }
    }
}